/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package saphre.demo;

import util.Interval;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import saphre.concordance.Concordance;
import saphre.concordance.ConcordanceOverlap;
import saphre.core.SaphreBase;
import saphre.core.SaphreCommandInterpreter;
import saphre.core.Store;
import saphre.core.SuffixArray;

/**
 * The main interface to SAPHRE - Suffix Arrays for Phrase Extraction.
 * @authors Dale Gerdemann, Niko Schenk
 *
 * For example run with:
 * 
 * ########
 * java Saphre 
 * > load corpus 
 * > run command 
 * 
 * ########
 * e.g., load corpus and extract gappy phrases:
 * java Saphre
 * > load:path/to/your/corpus/corpusname
 * > show:gappy
 * 
 * ########
 * of: compute ngram statistics for all repeats in the corpus
 * java Saphre
 * > load:path/to/your/corpus/corpusname
 * > show:repeats 
 * 
 * ########
 * or: query data to find the word "wonderful"
 * java Saphre
 * > load:path/to/your/corpus/corpusname
 * > wonderful
 * 
 * 
 *
 */
public class Saphre {
    
    public static String corpusPath = "";

    /**
     * Test client to run Saphre in interactive mode.
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        String encoding = "latin1";
        Scanner reader = new Scanner(System.in, encoding);

        //System.out.println("Console reader encoding: " + encoding);
        System.out.println("*** Running SAPHRE in interactive mode ***");
        System.out.println("Please, specify the path to your corpus.\n");


        // Setup data structures.
        Store store = null;
        int N = -1;
        SuffixArray sa = null;
        SuffixArray pa = null;
        Interval top = null;
        Concordance concord = null;
        ConcordanceOverlap concordOver = null;

        // query
        String q;
        boolean terminate = false;
        boolean corpusLoaded = false;

        // Run Saphre in interactive mode.
        while (true) {
            System.out.print("saphre> ");
            q = reader.nextLine();
            if ("quit".equals(q) || "exit".equals(q) || "q()".equals(q)) {
                terminate = true;
                System.out.println("Ciao!");
            }

            if (terminate) {
                break;
            }
            // User just pressed enter.
            if (q.equals("")) {
                continue;
            }
            
            Matcher mTxt;
            // Load a corpus from disc.
            Pattern load =
                    Pattern.compile("\\s*load\\s*:\\s*(.+)?");
            
            // TODO: Load corpus from serialized data files.
            Pattern loadSer =
                    Pattern.compile("\\s*loadSer\\s*:\\s*(.+)?");
            mTxt = load.matcher(q);
            if (mTxt.matches()) {
                terminate = false;
                if (mTxt.group(0) != null) {
                    corpusLoaded = true;
                    corpusPath = mTxt.group(0).substring(5);
                    if (corpusPath.charAt(corpusPath.length() - 1) == '/') {
                        corpusPath = corpusPath.substring(0, corpusPath.length() - 1);
                    }
                }

                
                // Setup data structures.
                // By default, do serialize the data.
                SaphreBase saphreBase = new SaphreBase(null, corpusPath,
                        corpusPath + "/../loadOptions/regex",
                        corpusPath + "/../loadOptions/norm",
                        corpusPath + "/../loadOptions/boundary", true, null);

                // Generate suffix array and components
                // and serialize data files.
                saphreBase.invoke();

                store = saphreBase.getStore();
                N = saphreBase.getN();
                sa = saphreBase.getSa();
                top = saphreBase.getTop();
                concord = saphreBase.getConcord();
                concordOver = saphreBase.getConcordOver();

                continue;
            }
            
            if(!corpusLoaded) {
                System.out.println("Please specify a proper corpus (path) first.");
            }
            else {
                // Everything is alright.
                // Execute query on the corpus data.
                System.out.println("Interpreting your query --> " + q + " <--");
                SaphreCommandInterpreter saphreCommandInterpreter = new SaphreCommandInterpreter();
                saphreCommandInterpreter
                    .interpretCommand(store, null, N, sa, pa, top, concord, concordOver, q, args);
            }
        }
    }
}
